﻿@using KPIS.GERP.Models.ICM
@model IEnumerable<KPIS.GERP.Models.ICM.RequestParcelDtl>

@{
    if (ViewBag.NoLayout as bool? ?? false)
    {
        Layout = null; 
    }


    int id = KPIS.GERP.WEB.GIMS.Class.GimsMembership.CurrentEmpId;
    var intRequestId = ViewBag.RequestId;

    var service = new KPIS.GERP.WEB.GIMS.RequestParcelService.RequestParcelServiceClient();
    var modelParcel = service.SelectRequestParcel(new KPIS.GERP.Models.ICM.RequestParcel { ReqParcelId = intRequestId, RecordStatus = "!D" }, new[] { "Project", "WarehousePermit.Warehouse", "WarehousePermit", "JobSite" }).FirstOrDefault();

    int intWhId = Convert.ToInt32(modelParcel.WarehousePermit.WhId);

    ViewBag.Title = "รายละเอียดรายการขอเบิก [" +  modelParcel.IcmReqCode + "]";
}

@Html.Partial("_DetailsFormHeader", modelParcel)

    <p class="row-fluid">
    <div>
        @if (ViewBag.NoLayout == false)
        {
            <table>
                <tr>
                    <td>
                        @Html.ButtonViewAll()
                    </td>
                    <td>
                        @if (modelParcel.RecordStatus == "U" || modelParcel.RecordStatus == "W" || modelParcel.RecordStatus == "R")
{
    <a class="btn btn-small btn-success" data-rel="tooltip" href="@Url.Action("Edit", new { id = modelParcel.ReqParcelId })"
       role="button" data-original-title="" title="แก้ไข @modelParcel.IcmReqCode"><i class="icon-edit bigger-120"></i>แก้ไข</a>
}

                    </td>
                    <td>
                        @if (new[] { "W", "R", "U" }.Contains(modelParcel.RecordStatus))
                        {

                            @Html.ActionLink(" เพิ่มการเบิกวัสดุ/ครุภัณฑ์", "CreateDetails", new { id = ViewBag.RequestId }
                                , new { @class = "btn btn-small btn-success icon-plus-sign-alt", role = "button" })
                             
                            @Html.ButtonDeleteAll()
                        } 
                    </td>
                    <td>
                        <button type="button" onclick="window.location.href ='@Url.Action("ReportPar", new { ReqId = modelParcel.ReqParcelId})'"
                                class="btn button-standard-width btn-small btn-blue"
                                data-rel="tooltip" role="button">
                            <i class="icon-file bigger-120"></i>&nbsp;&nbsp;รายงานใบเบิก
                        </button>
                    </td>
                    <td>
                        @using (Html.BeginForm("CreateAskApproval", "AskForApproval", FormMethod.Post, new {id = "formApprove", @class = "form-horizontal"}))
                        {
                            if (modelParcel.RecordStatus == RecordStatus.WaitStatus || modelParcel.RecordStatus == RecordStatus.ReturnedStatus || modelParcel.RecordStatus == RecordStatus.UsedStatus)
                            {
                                //var typeSeqขอเบิก = (int)ApproveProcessEnum.ขออนุมัติขอเบิก;
                                /*
        AppPrcGrpId	Code	Name	        AppPrcId	Name
        6	        PP	คำขออนุมัติแจ้งความประสงค์	6	คำขออนุมัติแจ้งความประสงค์
        7	            ขอเบิกวัสดุครุภัณฑ์	    7	ขอเบิกวัสดุครุภัณฑ์                <-- Request Parcel View
        8	        PR	ขออนุมัติขอซื้อขอจ้าง	    8	ขออนุมัติขอซื้อขอจ้างเกิน๑๐๐๐๐๐บาท
        8	        PR	ขออนุมัติขอซื้อขอจ้าง	    9	ขออนุมัติขอซื้อขอจ้างไม่เกิน๑๐๐๐๐๐บาท
        10	        PO-Purchase	ขออนุมัติใบสั่ง	10	ขออนุมัติใบสั่งเกิน๕๐๐๐๐๐บาท
        10	        PO-Purchase	ขออนุมัติใบสั่ง	11	ขออนุมัติใบสั่งไม่เกิน๕๐๐๐๐๐บาท
        12	        BD	ขออนุมัติประกาศ	        12	ขออนุมัติประกาศ
        13	        PO-Contract	ขออนุมัติงานสัญ	13	ขออนุมัติงานสัญญา
        14	        DN	ขออนุมัติการตรวจรับ	    24	การตรวจรับไม่เกิน๕๐๐๐๐บาท
        14	        DN	ขออนุมัติการตรวจรับ	    25	การตรวจรับเกิน๕๐๐๐๐บาท */
                                @Html.AntiForgeryToken()
                                @Html.Hidden("requestTitle", modelParcel.RequestName)
                                @Html.Hidden("refId", modelParcel.ReqParcelId)
                                @Html.Hidden("AppPrcGrpId", 7)
                                @Html.Hidden("refCode", modelParcel.IcmReqCode)
                                @Html.Hidden("applyToEmpId", GimsMembership.CurrentEmpId)
                                @Html.Hidden("confirmAs", "P")
                                @Html.Hidden("approveAs", "A")
                                @Html.Hidden("amount", 0)

                                <button type="submit" id="btnAskApproval"
                                        class="btn button-standard-width btn-small btn-yellow"
                                        data-rel="tooltip"
                                        data-href="@Url.Action("CreateAskApproval", "AskForApproval")"
                                        style="margin:15px 0 0 0">
                                    <i class="icon-plus bigger-120"></i>&nbsp;&nbsp;ส่งเรื่องขอเบิก
                                </button>
                            }
                        }
                    </td>
                </tr>
            </table> 
        }
        
    </div>
</p>
    
    <div class="row-fluid">
        <div class="table-header span12">
            <div class="span10">
                @ViewBag.Title
            </div>
            
        </div>
    </div>

@using (Html.BeginForm("DeleteDtlAll", "RequestParcel", FormMethod.Post, new { id = "form", @class = "form-horizontal" }))
{ 
    <table id="table_request_parcel_dtl" class="table table-striped table-bordered table-hover" 
        aria-describedby="table_request_parcel_dtl_info">
        <thead>
            <tr>
                <th class="center">
                    <label>
                        <input type="checkbox" />
                        <span class="lbl"></span>
                    </label>

                </th>
                <th>@Html.DisplayNameFor(model => model.ItemName)</th>
                <th>@Html.DisplayNameFor(model => model.Quantity)</th>
                <th></th>
            </tr>
        </thead>

        <tbody>
            @foreach (var m in Model)
            {
                <tr>
                    <td class="center">
                        <label>
                            <input id="selAll" type="checkbox" name="deleteIds" value="@(m.ReqDtlId)"  />
                            <span class="lbl"></span>
                        </label>
                    </td>

                    <td>@Html.DisplayFor(r => m.ItemName)</td>
                    <td style="text-align: right;">@((m.Quantity ?? 0).ToString("#,##0.##"))</td>

                    <td class="td-actions">
                        <div class="hidden-phone visible-desktop btn-group">
                            @if (ViewBag.NoLayout == false && (new[]{"W", "R", "U"}.Contains(modelParcel.RecordStatus)))
                            {
                                @Html.ButtonEdit(new {id = m.ReqDtlId, WhId = intWhId}, true, "EditDetails")
                                @Html.ButtonDelete(new {id = m.ReqDtlId, intReqId = m.ReqId}, true, "DeleteDetails")
                            }
                            else if (ViewBag.NoLayout != true)
                            { 
                                <a class="btn btn-mini btn-success" data-rel="tooltip" role="button" 
                                   data-original-title="" title="ผ่านการอนุมัติแล้ว" disabled="disabled">
                                    <i class="icon-edit bigger-120"></i>
                                </a>

                                <a class="btn btn-mini btn-danger" data-rel="tooltip" role="button"
                                    data-original-title="" title="ผ่านการอนุมัติแล้ว" disabled="disabled">
                                    <i class="icon-trash bigger-120"></i>
                                </a>

                            }
                           
                        </div>
                    </td>
                </tr>
            }
        </tbody>
    </table>
}



@section scripts {

    <script type="text/javascript">
        $(function () {
            var oTable1 = $('#table_request_parcel_dtl').dataTable({
                "aoColumns": [
                  { "bSortable": false },
                  null, null,
                  { "bSortable": false }
                ]
            });
            $('table th input:checkbox').on('click', function () {
                var that = this;
                $(this).closest('table').find('tr > td:first-child input:checkbox')
                .each(function () {
                    this.checked = that.checked;
                    $(this).closest('tr').toggleClass('selected');
                });

            });

            $('[data-rel=tooltip]').tooltip();
        })

        $("#form").submit(function (e) {
            e.preventDefault();
            var currentForm = this;
            bootbox.confirm("คุณแน่ใจหรือไม่", function (c) {
                if (c) {
                    $.ajax({
                        url: "@Url.Action("DeleteDtlAll")",
                        type: "post",
                        dataType: "json",
                        data: $(currentForm).serialize(),
                        context: document.body,
                        success: function (s) {
                            if (s.success) {
                                location.reload();
                            }
                        },
                        error: function (xhr, status, error) {
                            alert("Status: " + status + ", Error: " + error + ", XHR: " + xhr.readyState);
                        },
                        statusCode: {
                            404: function () {
                                alert("Page not found");
                            }
                        }
                    });
                }
            })
            return false;
        });

    </script>
}